package controllers

import base.Message
import base.State
import com.jfinal.aop.Before
import com.jfinal.core.Controller
import controllers.interceptors.SuperUserTokenValidator
import services.AdminService

@Before(SuperUserTokenValidator::class)
class AdminController: Controller() {

    companion object{
        const val PARA_CHANGE_STATE_RECORD_ID = "id"
        const val PARA_CHANGE_STATE_RECORD_STATE = "state"
        const val PARA_QUERY_RECORD_CONTENT = "content"
        const val PARA_QUERY_RECORD_TYPE = "type"
        const val PARA_QUERY_USER_CONTENT = "content"
        const val PARA_QUERY_USER_TYPE = "type"
        const val PARA_MODIFY_PERMISSION_UER_ID = "userId"
        const val PARA_MODIFY_PERMISSION_PERMISSION = "permission"
        const val PARA_MODIFY_PERMISSION_VALUE = "value"
    }

    private val service: AdminService by lazy {
        AdminService(this)
    }

    fun login(){
        val username = get(UserController.PARA_SU_USERNAME)
        val password = get(UserController.PARA_SU_USER_PWD)
        if (username.isNullOrEmpty() || password.isNullOrEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_USER_USERNAME_OR_PWD_EMPTY))
            renderJson(message)
            return
        }
        service.superUserLogin(username, password)
    }

    fun reviewList(){
        service.findReviewingList()
    }

    fun change_record_state(){
        val recordId = getInt(PARA_CHANGE_STATE_RECORD_ID)
        val state = getInt(PARA_CHANGE_STATE_RECORD_STATE)
        if (recordId == null || state == null){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_REQUEST_PARAS_ERROR))
            renderJson(message)
            return
        }
        service.changeRecordState(recordId, state)
    }

    fun queryRecord(){
        val content = get(PARA_QUERY_RECORD_CONTENT)
        val type = get(PARA_QUERY_RECORD_TYPE)
        if (content.isNullOrEmpty() || type.isNullOrEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_REQUEST_PARAS_ERROR))
            renderJson(message)
            return
        }
        service.queryRecord(content, type)
    }

    fun queryUser(){
        val content = get(PARA_QUERY_USER_CONTENT)
        val type = get(PARA_QUERY_USER_TYPE)
        if (content.isEmpty() || type.isEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_REQUEST_PARAS_ERROR))
            renderJson(message)
            return
        }
        service.queryUser(content, type)
    }

    fun modifyPermission(){
        val userId = getInt(PARA_MODIFY_PERMISSION_UER_ID)
        val permission = get(PARA_MODIFY_PERMISSION_PERMISSION)
        val value = getBoolean(PARA_MODIFY_PERMISSION_VALUE)
        if (userId == null || value == null || permission.isNullOrEmpty()){
            val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_REQUEST_PARAS_ERROR))
            renderJson(message)
            return
        }
        service.changeUserPermission(userId, permission, value)
    }
}